package com.smallrig.mall.template.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smallrig.mall.template.entity.MyInterval;
import com.smallrig.mall.template.service.IMyIntervalService;
import org.apache.shardingsphere.data.pipeline.core.execute.ExecuteEngine;
import org.apache.shardingsphere.infra.merge.MergeEngine;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lry
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/myInterval")
public class MyIntervalController {


    @Resource
    private IMyIntervalService myIntervalService;

    @GetMapping("save")
    public void testMyInterval(){
        MyInterval tInterval = new MyInterval();
        LocalDateTime now = LocalDateTime.now();
        tInterval.setCreateTime(now);
        myIntervalService.save(tInterval);

        for(int i=0;i<5;i++){
            now = now.minusMonths(6);
            tInterval.setCreateTime(now);
            myIntervalService.save(tInterval);
        }
    }

    @GetMapping("query")
    public void testQueryMyInterval() throws IOException {
        List<MyInterval> list = myIntervalService.lambdaQuery()
                .ge(MyInterval::getCreateTime, LocalDateTime.now().minusYears(1))
                .le(MyInterval::getCreateTime, LocalDateTime.now())
//                .groupBy(MyInterval::getId)
                .list();
        System.out.println(list);
    }

//    MergeEngine

    @GetMapping("sumAge")
    public void sumAge() throws IOException {
        QueryWrapper<MyInterval> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("IFNULL(SUM(age),0) as age");
        queryWrapper.select("SUM(age) as age");

        MyInterval one =  myIntervalService.getOne(queryWrapper);
        System.out.println(one);
    }


}
